//
//  PageStatePlaceHolderView.swift
//  ContactPoster
//
//  Created by TSYH on 2024/1/27.
//

import UIKit

public protocol PageStatePlaceHolder {
    var placeHolderImageName: String? { get }
    var placeHolderMessage: String? { get }
    var placeHolderTextColor: UIColor { get }
}

public extension PageStatePlaceHolder {
    var placeHolderTextColor: UIColor {
        return UIColor.white
    }
}

public class PageStatePlaceHolderView: UIView {
    lazy var stackView = UIStackView()
    lazy var imageView = UIImageView()
    lazy var messageLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        
        stackView.axis = .vertical
        stackView.spacing = 60
        stackView.distribution = .fill
        stackView.alignment = .center
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.right.equalToSuperview()
        }
        
        
        let view = UIView()
        stackView.addArrangedSubview(view)
        view.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
        }
        
        view.addSubview(imageView)
        imageView.contentMode = .scaleAspectFit
        imageView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.width.height.equalTo(40)
            make.centerX.equalToSuperview()
        }
        
        view.addSubview(messageLabel)
        messageLabel.snp.makeConstraints { make in
            make.top.equalTo(imageView.snp.bottom).offset(12)
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        messageLabel.font = .systemFont(ofSize: 12)
        messageLabel.textColor = .white
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func set(state: PageStatePlaceHolder) {
        if let imageName = state.placeHolderImageName,
            let image = UIImage(named: imageName) {
            imageView.isHidden = false
            imageView.image = image
            
            imageView.snp.remakeConstraints { make in
                make.top.equalToSuperview()
                make.size.equalTo(image.size)
                make.centerX.equalToSuperview()
            }
        } else {
            imageView.isHidden = true
        }
        
        messageLabel.textColor = state.placeHolderTextColor
        if let message = state.placeHolderMessage, !message.isEmpty {
            messageLabel.text = message
            messageLabel.isHidden = false
        } else {
            messageLabel.isHidden = true
        }
    }
    
    var actionViews = [UIView]()
    func updateActionViews(_ views: [UIView]) {
        actionViews.forEach({ $0.removeFromSuperview() })
        
        if let first = views.first {
            stackView.addArrangedSubview(first)
            first.snp.makeConstraints { make in
                make.size.equalTo(first.frame.size)
            }
        }
    }
}
